#include <stdint.h>
#include "lv_conf.h"
#include "../../board.h"
#include "lv_drv_conf.h"

#if USE_LOCAL_MFP
#define CRANE_MFPR_BASE    0xD401E000
#define MFP_CONFIG_RETRY   3

void mfp_config(const uint32_t *mfp_cfgs)
{
    uint32_t *p_mfpr = NULL;
    uint32_t cfg_val, val, val_r;
    uint32_t i = 0;

    do {
        cfg_val = *mfp_cfgs++;
        /* exit if End of configuration table detected */
        if (cfg_val == MFP_EOC) {
            break;
        }

        p_mfpr = (uint32_t *)(CRANE_MFPR_BASE
                              + MFP_REG_GET_OFFSET(cfg_val));

        /* Write a mfg register as per configuration */
        val = 0;
        if (cfg_val & MFP_VALUE_MASK) {
            val |= cfg_val & MFP_VALUE_MASK;
        }

        for(i = 0; i< MFP_CONFIG_RETRY; i++) {
            writel(val, p_mfpr);
            val_r = readl(p_mfpr);
            if(val == val_r) {
              break;
            }
        }

        if(i == MFP_CONFIG_RETRY) {
            printf("mfp config (%p = 0x%x) failed, reg val = 0x%x\n", p_mfpr, val, val_r);
        }
    } while (1);
}

#endif
